﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using Skyworth.CRM.Common;
using Skyworth.CRM.Service.IBLL;

namespace Skyworth.CRM.Web.BasicData
{
    public partial class SelectEmployees : System.Web.UI.Page
    {
        private IDepartmentBLL departmentBLL = IocContainer.Instance.Resolve<IDepartmentBLL>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {                
                BuildDepartmentTree();                
            }
        }

        private void BuildDepartmentTree()
        {
            DataTable dtDepartment = departmentBLL.GetAllDepartment();
            this.ddlDepartment.Items.Add(new ListItem("【全部部门】", "-1"));
            BindTreeViewToDropdownList(dtDepartment, 0, this.ddlDepartment, -1, "DepartmentName", "DepartmentId");
        }

        /// <summary>
        /// 绑定部门树形结构到DropdownList
        /// </summary>
        /// <param name="dtDepartment">部门DataTable,须包含DepartmentId，DepartmentName 字段</param>
        /// <param name="parentId"></param>
        /// <param name="ddlDept"></param>
        protected void BindTreeViewToDropdownList(DataTable dtDepartment, int parentId, DropDownList ddlDept, int count, string nameField, string valueField)
        {
            if (dtDepartment != null && dtDepartment.Rows.Count > 0)
            {
                DataRow[] drRoots = dtDepartment.Select("ParentId = " + parentId);
                count++;
                foreach (DataRow row in drRoots)
                {
                    int deptId = 0;
                    int.TryParse(row["DepartmentId"].ToString(), out deptId);
                    StringBuilder strText = new StringBuilder(string.Empty);

                    for (int i = 0; i < count; i++)
                    {
                        strText.Append(Server.HtmlDecode("&nbsp;&nbsp;&nbsp;"));
                    }
                    strText.Append(row[nameField].ToString());
                    ddlDept.Items.Add(new ListItem(strText.ToString(), row[valueField].ToString()));

                    BindTreeViewToDropdownList(dtDepartment, deptId, ddlDept, count, nameField, valueField);
                }


            }

        }

        [WebMethod]
        public static string GetEmpolyees(string Department, string EmpCno, string EmpName, string orderField, string orderValue, int pageIndex, int pagePerSize)
        {            
            string strResult;
            StringBuilder strWhere = new StringBuilder(" where 1=1 ");
            if (!string.IsNullOrEmpty(Department) && Department != "-1")
            {
                strWhere.Append(" and tbe.DeptID=" + Department);
            }
            if (!string.IsNullOrEmpty(EmpCno))
            {
                strWhere.Append(" and EmpCode like '%" + EmpCno + "%'");
            }
            if (!string.IsNullOrEmpty(EmpName))
            {
                strWhere.Append(" and EmpName  like '%" + EmpName + "%'");
            }
            StringBuilder strOrderBy = new StringBuilder(" Order by ");
            strOrderBy.Append(!string.IsNullOrEmpty(orderField) ? " " + orderField : "  EmpCode  ");
            strOrderBy.Append(!string.IsNullOrEmpty(orderValue) ? " " + orderValue : "  asc ");
            int totalRecords = 0;
            int totalPages = 0;
            IEmployeeBLL empBLL = IocContainer.Instance.Resolve<IEmployeeBLL>();
            DataTable dtEmps = empBLL.GetEmployeesByPage(strWhere.ToString(), strOrderBy.ToString(), pageIndex, pagePerSize, out totalRecords,
                                           out totalPages);
            strResult = SerializationHelper.DataTable2Json(dtEmps);
            return "{\"totalRecords\":\"" + totalRecords.ToString() + "\" ,\"totalPages\":\"" + totalPages.ToString() + "\",\"rows\":" +
                   strResult + " }";
        }
    }
}